<?php
Yii::import('zii.widgets.jui.CJuiDatePicker');
class CJuiDatePickerPlus extends CJuiDatePicker
{
    public $jUiBuildName = 'datepicker';
    
    public function registerCoreScripts(){
        parent::registerCoreScripts();
        Yii::app()->clientScript->registerCssFile(Yii::app()->request->baseUrl.'/js/lib/jQuery-Timepicker-Addon/jquery-ui-timepicker-addon.min.css');
        Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/lib/jQuery-Timepicker-Addon/jquery-ui-timepicker-addon.min.js');
    }
    
    public function run()
    {
        list($name,$id)=$this->resolveNameID();
    
        if(isset($this->htmlOptions['id']))
            $id=$this->htmlOptions['id'];
        else
            $this->htmlOptions['id']=$id;
        if(isset($this->htmlOptions['name']))
            $name=$this->htmlOptions['name'];
    
        if($this->flat===false)
        {
            if($this->hasModel())
                echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
            else
                echo CHtml::textField($name,$this->value,$this->htmlOptions);
        }
        else
        {
            if($this->hasModel())
            {
                echo CHtml::activeHiddenField($this->model,$this->attribute,$this->htmlOptions);
                $attribute=$this->attribute;
                $this->options['defaultDate']=$this->model->$attribute;
            }
            else
            {
                echo CHtml::hiddenField($name,$this->value,$this->htmlOptions);
                $this->options['defaultDate']=$this->value;
            }
    
            $this->options['altField']='#'.$id;
    
            $id=$this->htmlOptions['id']=$id.'_container';
            $this->htmlOptions['name']=$name.'_container';
    
            echo CHtml::tag('div',$this->htmlOptions,'');
        }
    
        $options=CJavaScript::encode($this->options);
        $js = "jQuery('#{$id}').{$this->jUiBuildName}($options);";
    
        if($this->language!='' && $this->language!='en')
        {
            $this->registerScriptFile($this->i18nScriptFile);
            $js = "jQuery('#{$id}').{$this->jUiBuildName}(jQuery.extend({showMonthAfterYear:false},jQuery.{$this->jUiBuildName}.regional['{$this->language}'],{$options}));";
        }
    
        $cs = Yii::app()->getClientScript();
    
        if(isset($this->defaultOptions))
        {
            $this->registerScriptFile($this->i18nScriptFile);
            $cs->registerScript(__CLASS__,$this->defaultOptions!==null?'jQuery.{$this->jUiBuildName}.setDefaults('.CJavaScript::encode($this->defaultOptions).');':'');
        }
        $cs->registerScript(__CLASS__.'#'.$id,$js);
    }
}